package com.robusta.cityuate.activities;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.LinearLayout;
import android.widget.Toast;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.robusta.cityuate.R;
import com.robusta.cityuate.base.CityuateFragmentMenuActivity;
import com.robusta.cityuate.services.DBClass;
import com.robusta.cityuate.services.LocationService;

public class RemindersListActivity extends CityuateFragmentMenuActivity {

	private LinearLayout listbody;
	private ActionBar mActionBar;
	

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		this.setContentView(R.layout.listing_with_delete);
		// startservice
		Intent intent = new Intent(RemindersListActivity.this,
				LocationService.class);
		startService(intent);
		// initDB
		initDB();
//		TextView header = (TextView) findViewById(R.id.list_title);
//		header.setText("Reminder");
		listbody = (LinearLayout) findViewById(R.id.list_body);
		// filling
		// Dummy Testing
		// String[] remind = {"dummy","0","0","Robusta"};
		// DBClass.InsertIntoTable(this, DBClass.TABLE_NAME_REMINDER,remind);
		// DBAccess
		ArrayList<ArrayList<String>> list = getAllReminders();
		System.out.println(list);
		for (int i = 0; i < list.size(); i++) {
			CustomItem item = new CustomItem(this);
			item.setValues(list.get(i).get(0), list.get(i).get(4), list.get(i)
					.get(1));
			listbody.addView(item);
		}
		if (listbody.getChildCount() == 0) {
			Toast.makeText(RemindersListActivity.this,
					"No RemindersListActivity", Toast.LENGTH_SHORT).show();
		}
		// Add
		Button add = (Button) findViewById(R.id.list_add);
		add.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View arg0) {
				RemindersListActivity.this.startActivity(new Intent(
						RemindersListActivity.this,
						CreateReminderActivity.class));
			}
		});
	

		mActionBar = getSupportActionBar();
		mActionBar.setTitle(R.string.reminders);
		
	}

	private void deleteReminders() {
		for (int i = 0; i < listbody.getChildCount(); i++) {
			if (((CheckBox) (((LinearLayout) listbody.getChildAt(i))
					.findViewById(R.id.check_for_action))).isChecked()) {
				deleteItemFromTable(((CustomItem) listbody.getChildAt(i))
						.getDBId());
				listbody.removeView(listbody.getChildAt(i));
				i--;
			}

		}
		if (listbody.getChildCount() == 0) {
			Toast.makeText(RemindersListActivity.this,
					"No RemindersListActivity", Toast.LENGTH_SHORT).show();
		}

	}

	private void deleteItemFromTable(String id) {
		DBClass.DeleteFromTable(this, DBClass.TABLE_NAME_REMINDER, id);
	}

	private ArrayList<ArrayList<String>> getAllReminders() {
		return DBClass.getTable(this, DBClass.TABLE_NAME_REMINDER);
	}

	private void initDB() {
		DBClass.CreateTables(this);
	}

	@Override
	protected void onResume() {
		listbody.removeAllViews();
		ArrayList<ArrayList<String>> list = getAllReminders();
		for (int i = 0; i < list.size(); i++) {
			CustomItem item = new CustomItem(this);
			item.setValues(list.get(i).get(0), list.get(i).get(4), list.get(i)
					.get(1));
			listbody.addView(item);
		}
		if (listbody.getChildCount() == 0) {
			Toast.makeText(RemindersListActivity.this,
					"No RemindersListActivity", Toast.LENGTH_SHORT).show();
		}
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getSupportMenuInflater().inflate(R.menu.reminders_option_menu, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case R.id.add:
			RemindersListActivity.this.startActivity(new Intent(
					RemindersListActivity.this,
					CreateReminderActivity.class));
			break;
		case R.id.delete_multi:
			deleteReminders();
			break;

		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}

}
